// pages/classify/classify.js
import api from '../../api/index.js'
const {request} = require('../../utils/wx-api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList:'',
    id:1005000,
    idx:0,
    currentCategory:'',
    subCategoryList:''
  },
  getData(e){
    console.log(e.currentTarget.dataset);
    let {idx} = e.currentTarget.dataset;
    let {id} = e.currentTarget.dataset;
    this.setData({
      idx,
      id
    })
    request({ url: api.current }, { id: this.data.id })
    .then(res => {
      console.log(res);
      let { currentCategory } = res.data.data;
      let { subCategoryList } = currentCategory;
      this.setData({
        currentCategory,
        subCategoryList
      })
      wx.setStorageSync('subCategoryList', subCategoryList);
    })
  },
  toGoodsList(e){  //跳转到商品列表页
  let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/goodsList/goodsList?id=${id}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    request({url:api.current},{id:this.data.id})
    .then(res=>{
      // console.log(res);
      let { currentCategory } = res.data.data;
      let { subCategoryList } = currentCategory;
      this.setData({
        currentCategory,
        subCategoryList
      })
      wx.setStorageSync('subCategoryList', subCategoryList);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let {categoryList} = wx.getStorageSync('indexData');
    this.setData({
      categoryList
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})